module I2C_CAM_Config (
//	Host Side
iCLK,
iRST_N,
//	I2C Side
I2C_SCLK,
I2C_SDAT
);

//	Host Side
input	iCLK;
input	iRST_N;
//	I2C Side
output I2C_SCLK;
inout	I2C_SDAT;
//	Internal Registers/Wires
reg [15:0] mI2C_CLK_DIV;
reg [23:0] mI2C_DATA;
reg mI2C_CTRL_CLK;
reg mI2C_GO;
wire mI2C_END;
wire mI2C_ACK;
reg [15:0] LUT_DATA;
reg [5:0] LUT_INDEX;
reg [3:0] mSetup_ST;

//	Clock Setting
parameter CLK_Freq	=	50000000;	//	50	MHz
parameter I2C_Freq	=	20000;		//	20	KHz
//	LUT Data Number
parameter LUT_SIZE	=	8;
//	CAM Data Index
parameter	Dummy_DATA	=	0;
parameter PROGRESSIVE_SCAN = 1;
parameter RGB_IMAGE_PLANE = 2;
parameter WINDOW_SIZING_HS = 3;
parameter WINDOW_SIZING_HE	= 4;
parameter MANUAL_ADJUST_MODE_EN = 5;
parameter GAIN_SETTINGS = 6;
parameter EXPTIME_SETTING = 7;
parameter TRI_STATED_BUSES = 8;

/////////////////////	I2C Control Clock	////////////////////////
always@(posedge iCLK or negedge iRST_N)
begin
	if(!iRST_N)
	begin
		mI2C_CTRL_CLK	<=	0;
		mI2C_CLK_DIV	<=	0;
	end
	else
	begin
		if( mI2C_CLK_DIV	< (CLK_Freq/I2C_Freq) )
		mI2C_CLK_DIV	<=	mI2C_CLK_DIV+1'b1;
		else
		begin
			mI2C_CLK_DIV	<=	0;
			mI2C_CTRL_CLK	<=	~mI2C_CTRL_CLK;
		end
	end
end

////////////////////////////////////////////////////////////////////
I2C_Controller u0 (
.CLOCK(mI2C_CTRL_CLK),		//	Controller Work Clock
.I2C_SCLK(I2C_SCLK),			//	I2C CLOCK
.I2C_SDAT(I2C_SDAT),			//	I2C DATA
.I2C_DATA(mI2C_DATA),		//	DATA:[SLAVE_ADDR,SUB_ADDR,DATA]
.GO(mI2C_GO),      			//	GO transfor
.END(mI2C_END),				//	END transfor 
.ACK(mI2C_ACK),				//	ACK
.RESET(iRST_N)
);
////////////////////////////////////////////////////////////////////

//////////////////////	Config Control	////////////////////////////
always@(posedge mI2C_CTRL_CLK or negedge iRST_N)
begin
	if(!iRST_N)
	begin
		LUT_INDEX	<=	0;
		mSetup_ST	<=	0;
		mI2C_GO		<=	0;
	end
	else
	begin
		if(LUT_INDEX<LUT_SIZE)
		begin
			case(mSetup_ST)
			0:	begin
					mI2C_DATA	<=	{8'h42,LUT_DATA};
					mI2C_GO		<=	1;
					mSetup_ST	<=	1;
				end
			1:	begin
					if(mI2C_END)
					begin
						if(!mI2C_ACK)
						mSetup_ST	<=	2;
						else
						mSetup_ST	<=	0;							
						mI2C_GO		<=	0;
					end
				end
			2:	begin
					LUT_INDEX	<=	LUT_INDEX+1'b1;
					mSetup_ST	<=	0;
				end
			endcase
		end
	end
end
////////////////////////////////////////////////////////////////////
/////////////////////	Config Data LUT	  //////////////////////////	
always
begin
	case(LUT_INDEX)
		//	CAM Config Data
		Dummy_DATA					:	LUT_DATA	<=	16'h0000;
		PROGRESSIVE_SCAN			:	LUT_DATA	<=	16'h2824;
		//RGB_IMAGE_PLANE			:	LUT_DATA <= 16'h122C;	//RGB
		RGB_IMAGE_PLANE			:	LUT_DATA <= 16'h1224;	//YCrCb
		//WINDOW_SIZING_HS			:	LUT_DATA <= 16'h174B;	//83.pixeltől
		//WINDOW_SIZING_HE			:	LUT_DATA <= 16'h18C3;	//562. pixelig
		MANUAL_ADJUST_MODE_EN	: 	LUT_DATA	<=	16'h1300;
		GAIN_SETTINGS				:	LUT_DATA <= 16'h0000;
		EXPTIME_SETTING			:	LUT_DATA <= 16'h1080;
		TRI_STATED_BUSES			:	LUT_DATA <= 16'h1305;
		default:		LUT_DATA	<=	16'h0000;
	endcase
end
////////////////////////////////////////////////////////////////////
endmodule
